import os
import sys

from src.Port import Port

# 添加项目根目录到 Python 路径
current_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(current_dir)

from flask import Flask
from flask_cors import CORS
from src.routes import init_routes
from src.config import HOST, PORT, DEBUG
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

app = Flask(__name__, 
           static_folder='../static',  # 设置静态文件目录
           static_url_path='')  # 设置静态文件URL前缀为空

# 配置CORS，允许所有来源和所有方法
CORS(app, resources={
    r"/*": {
        "origins": "*",
        "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
        "allow_headers": ["Content-Type", "Authorization"]
    }
})

init_routes(app)

if __name__ == '__main__':
    print(f"服务器启动在 http://{HOST}:{PORT}")
    try:
        serial_port = Port(24,"com3","230400")
        print(f"串口开启成功")
    except Exception:
        print(f"串口开启失败")
    app.run(host=HOST, port=PORT, debug=DEBUG) 